<template>
	<view class="container">
		<view class="panel-header padding-top solid-bottom">
			<view class="g-goBack" @click="$utils.toBack()">
				<view class="cuIcon-back"></view>
			</view>
			<view class="text-cut">新增合伙人</view>
		</view>
		<block v-if="editStep == 1">
			<form>
				<view class="cu-form-group">
					<view class="title">手机号</view>
					<input placeholder="请填写(必填项)" v-model="info.phone"></input>
				</view>
			</form>
			<view class="cu-bar btn-group margin-top">
				<button class="cu-btn bg-grey shadow-blur round" @click="$utils.toBack()">取消</button>
				<button class="cu-btn bg-orange shadow-blur round" @click="btnFindUser()">查询用户</button>
			</view>
		</block>
		<block v-else>
			<form>
			    <view class="cu-form-group">
					<view class="title">手机号</view>
					<view>{{info.phone}}</view>
			    </view>
				<view class="cu-form-group" v-if="editUserName">
					<view class="title">姓名</view>
					<input placeholder="请填写(必填项)" v-model="info.nickname"></input>
				</view>
				<view class="cu-form-group" v-else>
					<view class="title">姓名</view>
					<view>{{info.nickname}}</view>
				</view>
				<view class="cu-form-group">
					<view class="title">备注信息</view>
					<input placeholder="请填写(非填项)" v-model="info.note"></input>
				</view>
				<view class="cu-bar btn-group margin-top">
				    <button class="cu-btn bg-grey shadow-blur round" @click="btnUpEdit()">上一步</button>
					<button class="cu-btn bg-orange shadow-blur round" @click="btnSaveSubmit()">保存</button>
				</view>
			</form>
		</block>
	</view>
</template>

<script>
	const wx = require('@/js_sdk/jweixin.js'); 
	export default {
		data() {
			return {
				editStep:1,
				editUserName:true,
				info:{}
			};
		},
		onLoad(option) {
			
		},
		onShow(){
			
		},
		methods: {
			//根据手机号查询用户
			btnFindUser:function(){
				console.log(this.info.phone);
				if(!this.info.phone){
					this.errorMsg = '请填写手机号';
					this.errorModal = true;
					return;
				}
				
				const api = this.$request;
				api.post(api.url.commUserFind,{
					phone:this.info.phone
				},res=>{
					console.log(res);
					if(res.code==200){
						this.editUserName = false;
						this.info.userId = res.body.userId;
						this.info.nickname = res.body.nickname;
						this.editStep = 2;
					}else if(res.code == 196){
						uni.showModal({
							title:'温馨提示',
							content:res.msg,
							showCancel:false
						})
						return
					}else{
						this.editUserName = true;
						this.editStep = 2;
					}
				})
			},
			btnUpEdit:function(){
				this.info.nickname = null;
				this.editStep = 1;
			},
			btnSaveSubmit(){
				if(!this.info.phone){
					uni.showModal({
						title:'温馨提示',
						content:'请输入手机号',
						showCancel:false
					})
					return
				}
				
				let info = this.info;
				info.roleType = 3;
				
				const _self = this;
				const api = this.$request;
				api.post(api.url.teamAdd,info,res=>{
					console.log(res)
					if(res.code==200){
						uni.showModal({
							content:'保存成功',
							showCancel:false,
							complete:function(res){
								uni.navigateBack();
							}
						})
					}else if(res.msg){
						uni.showModal({
							content:res.msg,
							showCancel:false
						})
					}else{
						uni.showToast({
							title:'操作失败',
							icon:'error'
						})
					}
				})
			}
		}
	}
</script>

<style lang="less">
.container .panel-header{height:90rpx;width:100vw;font-size:28rpx;background-color:#fff;}
.container .panel-header .text-cut{width:75%;margin:auto;text-align:center;}
.container .cu-form-group .title {min-width: calc(5em + 30rpx);}
.container input{text-align:right;}
</style>
